<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>search</title>
<script src="./js/jQuery.js"></script>
<script src="./js/jQueryUI/ui.tablesorter.js"></script>
<link rel="css/stylesheet" href="style.css"></link>

<script>
  $(document).ready(function() {
    $("table").tablesorter({
      sortList:[[7,0],[4,0]],
      debug: false,        // if true, useful to debug Tablesorter issues
      headers: {
        0: { sorter: false },  
        5: { sorter: false },
        6: { sorter: false },
        7: { sorter: 'text'}   
      }
    });
  });
  $(function(){
  	$('#select').click(function(){
  		$('#tradera').toggle();
  		$('#cse').toggle();
  	});
  });
</script>

<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<!--><script type="text/javascript">
  google.load('search', '1', {language : 'en', style : google.loader.themes.MINIMALIST});
  google.setOnLoadCallback(function() {
    var customSearchControl = new google.search.CustomSearchControl('016707586225017875167:qxelu9zmts8');
    customSearchControl.setResultSetSize(google.search.Search.SMALL_RESULTSET);
    customSearchControl.draw('cse');
  }, true);
</script> -->
 <style type="text/css">
  .gsc-control-cse {
    font-family: Arial, sans-serif;
    border-color: #666666;
    background-color: #FFFFFF;
  }
  input.gsc-input {
    border-color: #777777;
  }
  input.gsc-search-button {
    border-color: #333333;
    background-color: #333333;
  }
  .gsc-tabHeader.gsc-tabhInactive {
    border-color: #777777;
    background-color: #777777;
  }
  .gsc-tabHeader.gsc-tabhActive {
    border-color: #333333;
    background-color: #333333;
  }
  .gsc-tabsArea {
    border-color: #333333;
  }
  .gsc-webResult.gsc-result {
    border-color: #FFFFFF;
    background-color: #FFFFFF;
  }
  .gsc-webResult.gsc-result:hover {
    border-color: #000000;
    background-color: #FFFFFF;
  }
  .gs-webResult.gs-result a.gs-title:link,
  .gs-webResult.gs-result a.gs-title:link b {
    color: #444444;
  }
  .gs-webResult.gs-result a.gs-title:visited,
  .gs-webResult.gs-result a.gs-title:visited b {
    color: #444444;
  }
  .gs-webResult.gs-result a.gs-title:hover,
  .gs-webResult.gs-result a.gs-title:hover b {
    color: #444444;
  }
  .gs-webResult.gs-result a.gs-title:active,
  .gs-webResult.gs-result a.gs-title:active b {
    color: #777777;
  }
  .gsc-cursor-page {
    color: #444444;
  }
  a.gsc-trailing-more-results:link {
    color: #444444;
  }
  .gs-webResult.gs-result .gs-snippet {
    color: #333333;
  }
  .gs-webResult.gs-result .gs-visibleUrl {
    color: #000000;
  }
  .gs-webResult.gs-result .gs-visibleUrl-short {
    color: #000000;
  }
  .gs-webResult.gs-result .gs-visibleUrl-short {
    display: none;
  }
  .gs-webResult.gs-result .gs-visibleUrl-long {
    display: block;
  }
  .gsc-cursor-box {
    border-color: #FFFFFF;
  }
  .gsc-results .gsc-cursor-page {
    border-color: #777777;
    background-color: #FFFFFF;
  }
  .gsc-results .gsc-cursor-page.gsc-cursor-current-page {
    border-color: #333333;
    background-color: #333333;
  }
  .gs-promotion.gs-result {
    border-color: #CCCCCC;
    background-color: #E6E6E6;
  }
  .gs-promotion.gs-result a.gs-title:link {
    color: #0000CC;
  }
  .gs-promotion.gs-result a.gs-title:visited {
    color: #0000CC;
  }
  .gs-promotion.gs-result a.gs-title:hover {
    color: #444444;
  }
  .gs-promotion.gs-result a.gs-title:active {
    color: #00CC00;
  }
  .gs-promotion.gs-result .gs-snippet {
    color: #333333;
  }
  .gs-promotion.gs-result .gs-visibleUrl,
  .gs-promotion.gs-result .gs-visibleUrl-short {
    color: #00CC00;
  }
</style> 

<link rel="stylesheet" href="./css/flora.all.css" type="text/css" media="screen" title="Flora (Default)"> 




     
</head>
<body>

<form action="search.php" method="post">
<table cellpadding="2" border="0">
  <tr>
    <th>Query</th>
    <th>Site to Search</th>
    <th>Max Price</th>
    <th>Items per range</th>
    <th>Debug</th>
  </tr>
  <tr>
   <td><input type="text" name="Query" value="Search"></td>
    <td>
    <select name="GlobalID">
      <option selected value="EBAY-US">America</option>
      <option value="EBAY-ENCA">Canada</option>
      <option value="EBAY-GB">Great Britain</option>
      <option value="EBAY-AU">Australia</option>
      <option value="EBAY-AT">Austria</option>
      <option value="EBAY-FRBE">Belgium(French)</option>
      <option value="EBAY-FR">France</option>
      <option value="EBAY-DE">Germany</option>
      <option value="EBAY-MOTOR">Motor</option>
      <option value="EBAY-IT">Italy</option>
	  <option value="EBAY-NLBE">Belgium(Dutch)</option>
	  <option value="EBAY-NL">Netherlands</option>
	  <option value="EBAY-ES">Spain</option>
	  <option value="EBAY-CH">Switzerland</option>
      <option value="EBAY-HK">Hong Kong</option>
      <option value="EBAY-IN">India</option>
	  <option value="EBAY-IE">Ireland</option>
	  <option value="EBAY-MY">Malaysia</option>
	  <option value="EBAY-PH">Philippines</option>
      <option value="EBAY-PL">Poland</option>
      <option value="EBAY-SG">Singapore</option>
	  <option value="EBAY-SE">Sweden</option>
      <option value="EBAY-AU">Australia - EBAY-AU - AUD</option>
      <option value="EBAY-FRCA">Canada (French)</option>
      <option value="EBAY-ENCA">Canada (English) - EBAY-ENCA - CAD</option>
      <option value="EBAY-DE">Germany - EBAY-DE - EUR</option>
      <option value="EBAY-GB">United Kingdom - EBAY-GB - GBP</option>
      <option value="EBAY-US">United States - EBAY-US - USD</option>
    </select>
    </td>
    <td><input type="text" name="MaxPrice" value="500"></td>
    <td>
    <select name="ItemsPerRange">
      <option value="1">1</option>
      <option value="2">2</option>
      <option selected value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      </select>
    </td>
    <td>
    <select name="Debug">
      <option value="1">true</option>
      <option selected value="0">false</option>
      </select>
    </td>
 </tr> 
  <tr>
    <td colspan="2" align="center"><INPUT type="submit" name="submit" value="Search">
    </td>
  </tr>

</table>
</form>


<?php

require_once('DisplayUtils.php');  // functions to aid with display of information

error_reporting(E_ALL);  // turn on all errors, warnings and notices for easier debugging

$results = '';

if(isset($_POST['Query']))
{
  $endpoint = 'http://svcs.ebay.com/services/search/FindingService/v1';  // URL to call
  $responseEncoding = 'XML';   // Format of the response

  $safeQuery = urlencode (utf8_encode($_POST['Query']));
  $site  = $_POST['GlobalID'];

  $priceRangeMin = 0.0;
  $priceRangeMax = $_POST['MaxPrice'];
  $itemsPerRange = $_POST['ItemsPerRange'];
  $debug = (boolean) $_POST['Debug'];

  $rangeArr = array('Low-Range', 'Mid-Range', 'High-Range'); 

  $priceRange = ($priceRangeMax - $priceRangeMin) / 3;  // find price ranges for three tables
  $priceRangeMin =  sprintf("%01.2f", 0.00);
  $priceRangeMax = $priceRangeMin;  // needed for initial setup

  foreach ($rangeArr as $range)
  {
    $priceRangeMax = sprintf("%01.2f", ($priceRangeMin + $priceRange));
    $results .=  "<h2>$range : $priceRangeMin ~ $priceRangeMax</h2>\n";
    // Construct the FindItems call
    $apicall = "$endpoint?OPERATION-NAME=findItemsAdvanced"
         . "&SERVICE-VERSION=1.0.0"
         . "&GLOBAL-ID=$site"
         . "&SECURITY-APPNAME=ushise9d6-b979-4de5-a69d-bb1bf903677" //replace with your app id
         . "&keywords=$safeQuery"
        . "&paginationInput.entriesPerPage=$itemsPerRange"
         . "&sortOrder=BestMatch"
         . "&itemFilter(0).name=ListingType"
         . "&itemFilter(0).value=FixedPrice"
         . "&itemFilter(1).name=MinPrice"
         . "&itemFilter(1).value=$priceRangeMin"
         . "&itemFilter(2).name=MaxPrice"
         . "&itemFilter(2).value=$priceRangeMax"
         . "&affiliate.networkId=9"  // fill in your information in next 3 lines
         . "&affiliate.trackingId=1234567890"
         . "&affiliate.customId=456"
         . "&RESPONSE-DATA-FORMAT=$responseEncoding";

    if ($debug) {
      print "GET call = $apicall <br>";  // see GET request generated
    }
    // Load the call and capture the document returned by the Finding API
    $resp = simplexml_load_file($apicall);

    // Check to see if the response was loaded, else print an error
    // Probably best to split into two different tests, but have as one for brevity
    if ($resp && $resp->paginationOutput->totalEntries > 0) {
      $results .= 'Total items : ' . $resp->paginationOutput->totalEntries . "<br />\n";
      $results .= '<table id="example" class="tablesorter" border="0" cellpadding="0" cellspacing="1">' . "\n";
      $results .= "<thead><tr><th /><th>Title</th><th>Price &nbsp; &nbsp; </th><th>Shipping &nbsp; &nbsp; </th><th>Total &nbsp; &nbsp; </th><th><!--Currency--></th><th>Time Left</th><th>End Time</th></tr></thead>\n";

      // If the response was loaded, parse it and build links
      foreach($resp->searchResult->item as $item) {
        if ($item->galleryURL) {
          $picURL = $item->galleryURL;
        } else {
          $picURL = "http://pics.ebaystatic.com/aw/pics/express/icons/iconPlaceholder_96x96.gif";
        }
        $link  = $item->viewItemURL;
        $title = $item->title;

        $price = sprintf("%01.2f", $item->sellingStatus->convertedCurrentPrice);
        $ship  = sprintf("%01.2f", $item->shippingInfo->shippingServiceCost);
        $total = sprintf("%01.2f", ((float)$item->sellingStatus->convertedCurrentPrice
                      + (float)$item->shippingInfo->shippingServiceCost));

        // Determine currency to display - so far only seen cases where priceCurr = shipCurr, but may be others
        $priceCurr = (string) $item->sellingStatus->convertedCurrentPrice['currencyId'];
        $shipCurr  = (string) $item->shippingInfo->shippingServiceCost['currencyId'];
        if ($priceCurr == $shipCurr) {
          $curr = $priceCurr;
        } else {
          $curr = "$priceCurr / $shipCurr";  // potential case where price/ship currencies differ
        }

        $timeLeft = getPrettyTimeFromEbayTime($item->sellingStatus->timeLeft);
        $endTime = strtotime($item->listingInfo->endTime);   // returns Epoch seconds
        $endTime = $item->listingInfo->endTime;


        $results .= "<tr><td><a href=\"$link\"><img src=\"$picURL\"></a></td><td><a href=\"$link\">$title</a></td>"
             .  "<td>$price</td><td>$ship</td><td>$total</td><td>$curr</td><td>$timeLeft</td><td><nobr>$endTime</nobr></td></tr>";
	        }
      $results .= "</table>";
    }
    // If there was no response, print an error
    else {
      $results = "<p><i><b>No items found<b></i></p>";
    }
    $priceRangeMin = $priceRangeMax; // set up for next iteration
  } // foreach

} // if

?>


<?php echo $results;?>
</body>
</html>
